<?php

namespace app\index\controller;

use app\common\model\ArticleModel;
use app\common\model\CollegeModel;
use app\common\model\FruitModel;
use app\common\model\RequirementModel;
use app\common\model\SlideModel;
use app\common\model\TeacherModel;
use think\Db;

class IndexController extends BaseController
{
    public function index()
    {

        $user = json_decode(session('userInfo'),true);
        $issuer_type = isset($user['teacher_no']) ? 0 : 1;

        $count = ArticleModel::where('status', 1)->count();
        $articles = ArticleModel::where('status', 1)->field('id,title,excerpt,create_time,thumbs')
            ->order('id desc')
            ->limit(0, 5)
            ->all();
        foreach ($articles as $key => $vo) {
            $articles[$key]['link'] = url('index/article/index', 'id=' . $vo['id']);
        }
        $top = ArticleModel::where('status', 1)->where('is_top', 1)->order('update_time', 'desc')->find();
        $slide = get_slide('index_carousel');

        $length = 2;
        if ($issuer_type != 0){
            $length = 4;
        }
        $fruit = FruitModel::where('status', 1)
            ->with(['fruit_type', "teacher", "member"])
            ->where('audit_time', 'neq', null)
            ->order('audit_time', 'desc')
            ->limit(0, $length)->all();

        $requirements = RequirementModel::where('status', 1)
            ->with(['requirement_type', "teacher", "member"])
            ->where('audit_time', 'neq', null)
            ->order('audit_time', 'desc')
            ->limit(0, 2)->all();

        $s_colleges = CollegeModel::where('status','1')->where('show_teacher','1')->order('update_time','desc')->limit(0,5)->all();
        foreach ($s_colleges as $key => $college){
            $teachers = TeacherModel::where('status','1')->where('college',$college['id'])->where('show_home','1')->limit(0,4)->all();
            $s_colleges[$key]['teachers'] = $teachers;
        }

        $f_colleges = CollegeModel::where('status','1')->where('show_fruit','1')->order('update_time','desc')->limit(0,5)->all();
        foreach ($f_colleges as $key => $college){
            $teachers = Db::name("fruit")->alias('f')->join('sk_teacher t','t.id = f.issuer','left')->where('f.status','1')->where('t.college',$college['id'])->where('f.show_home','1')->field("f.*")->limit(0,4)->all();
            $f_colleges[$key]['fruits'] = $teachers;
        }


        $r_colleges = CollegeModel::where('status','1')->where('show_requirement','1')->order('update_time','desc')->limit(0,5)->all();
        foreach ($r_colleges as $key => $college){
            $teachers = Db::name("requirement")->alias('f')->join('sk_teacher t','t.id = f.issuer','left')->where('f.status','1')->where('t.college',$college['id'])->where('f.show_home','1')->field("f.*")->limit(0,4)->all();
            $r_colleges[$key]['requirements'] = $teachers;
        }

        $seList = TeacherModel::where("college",21)->limit(0,5)->order('sort','desc')->all();

        $this->assign([
            'count' => $count,
            'articles' => $articles,
            'slide' => $slide,
            'top' => $top,
            'fruits' => $fruit,
            'requirements' => $requirements,
            'colleges' => $s_colleges,
            'fruitColleges' => $f_colleges,
            'requirementColleges' => $r_colleges,
            'seList' => $seList,


        ]);//,'slide'=>$slide
        if (is_mobile()){
            return $this->fetch('/mobile/index');
        }
        return $this->fetch('/index');
    }

    public function get_article_flow()
    {
        $res['code'] = 1;
        $res['msg'] = 'success';

        $count = ArticleModel::where('status', 1)->count();

        $page = $this->request->param('page');
        $articles = ArticleModel::where('status', 1)
            ->field('id,title,excerpt,create_time')
            ->order('id desc')
            ->limit($page * 10, 10)
            ->all();
        foreach ($articles as $key => $vo) {
            $articles[$key]['link'] = url('index/article/index', 'id=' . $vo['id']);
        }
        $res['data'] = $articles;
        $res['pages'] = $count / 10;
        return $res;
    }

    /**
     * 联系我们
     * @return mixed
     */
    public function contact()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $res['code'] = 1;
            $res['msg'] = '感谢您的留言！';

            $data['create_time'] = date('Y-m-d H:i:s', time());

            $result = Db::name('contact')->insert($data);

            if ($result === false) {
                $res['code'] = 0;
                $res['msg'] = '添加失败！';
            }

            return json($res);
        }

        return $this->fetch('/contact');
    }

}
